package com.ecommerce.controller;

import com.ecommerce.entity.ProductImage;
import com.ecommerce.repository.ProductImageRepository;
import com.ecommerce.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/product-images")
@CrossOrigin(origins = "*")
public class ProductImageController {

    @Autowired
    private ProductImageRepository productImageRepository;

    @Autowired
    private ProductRepository productRepository;

    @GetMapping("/product/{productId}")
    public ResponseEntity<Map<String, Object>> getProductImages(@PathVariable Long productId) {
        try {
            List<ProductImage> images = productImageRepository.findActiveImagesByProductId(productId);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("images", images);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("message", "获取商品图片失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getProductImageById(@PathVariable Long id) {
        try {
            Optional<ProductImage> image = productImageRepository.findById(id);
            if (image.isPresent()) {
                Map<String, Object> response = new HashMap<>();
                response.put("success", true);
                response.put("image", image.get());
                return ResponseEntity.ok(response);
            } else {
                Map<String, Object> errorResponse = new HashMap<>();
                errorResponse.put("success", false);
                errorResponse.put("message", "图片不存在");
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("message", "获取图片失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }

    @PostMapping
    public ResponseEntity<Map<String, Object>> createProductImage(@RequestBody ProductImage productImage) {
        try {
            ProductImage savedImage = productImageRepository.save(productImage);
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "图片创建成功");
            response.put("image", savedImage);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("message", "创建图片失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateProductImage(@PathVariable Long id, @RequestBody ProductImage productImage) {
        try {
            Optional<ProductImage> existingImage = productImageRepository.findById(id);
            if (existingImage.isPresent()) {
                ProductImage image = existingImage.get();
                image.setImageUrl(productImage.getImageUrl());
                image.setImageName(productImage.getImageName());
                image.setAltText(productImage.getAltText());
                image.setSortOrder(productImage.getSortOrder());
                image.setIsMain(productImage.getIsMain());
                image.setIsActive(productImage.getIsActive());
                
                ProductImage updatedImage = productImageRepository.save(image);
                Map<String, Object> response = new HashMap<>();
                response.put("success", true);
                response.put("message", "图片更新成功");
                response.put("image", updatedImage);
                return ResponseEntity.ok(response);
            } else {
                Map<String, Object> errorResponse = new HashMap<>();
                errorResponse.put("success", false);
                errorResponse.put("message", "图片不存在");
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("message", "更新图片失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteProductImage(@PathVariable Long id) {
        try {
            Optional<ProductImage> image = productImageRepository.findById(id);
            if (image.isPresent()) {
                productImageRepository.deleteById(id);
                Map<String, Object> response = new HashMap<>();
                response.put("success", true);
                response.put("message", "图片删除成功");
                return ResponseEntity.ok(response);
            } else {
                Map<String, Object> errorResponse = new HashMap<>();
                errorResponse.put("success", false);
                errorResponse.put("message", "图片不存在");
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            Map<String, Object> errorResponse = new HashMap<>();
            errorResponse.put("success", false);
            errorResponse.put("message", "删除图片失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(errorResponse);
        }
    }
} 